<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Home extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('captcha');
    }

    public function index() {
        //redireccion segura
        if ($this->session->userdata('access')) {
            redirect('xpanel');
        }

        $vals = array(
            'img_path' => './assets/captcha/',
            'img_url' => base_url() . "assets/captcha/",
            'font_path' => './assets/captcha/fonts/OpenSans-Regular.ttf',
            'img_width' => '250',
            'img_height' => 30,
            'expiration' => 7200
        );

        $cap = create_captcha($vals);
        // se agrega el captcha a la base de datos
        $this->cc_model_b->insertCaptcha($cap['time'], $this->input->ip_address(), $cap['word']);
        $data['cap'] = $cap;
        $this->load->view('login/login_view', $data);
    }

    public function x() {

        # SET VALIDATION RULES
        $this->form_validation->set_rules('captcha', 'Captcha', 'required|callback_captcha_check');
        $this->form_validation->set_rules('user', 'username', 'required|trim|xss_clean|min_length[6]|max_length[15]');
        $this->form_validation->set_rules('pass', 'password', 'required|trim|xss_clean|min_length[6]|max_length[20]');

        # has the form been submitted and with valid form info (not empty values)
        if (!$this->form_validation->run()) {
            redirect('home');
        }

        # autenticacion segura
        $usuario = $this->security->xss_clean($this->input->post('user'));
        $pass = $this->security->xss_clean($this->input->post('pass'));

        if ($this->verifica($usuario, $pass)) {
            $this->cc_model_b->deleteCaptcha($this->input->ip_address());
            $admin = $this->session->userdata('admin');
            $l_ip = $this->input->ip_address();
            $l_nombre = $admin['a_nombre'];
            
            $this->cc_model_b->regAcceso($l_ip,$l_nombre);
            switch ($admin['ta_tipo_administrador_id']) {
                #administrador
                case 1:
                    redirect('xpanel');
                    break;
                #Supervisor
                case 2:
                    redirect('spanel');
                    break;
            }
        } else {
            redirect('home');
        }
    }
    
    function captcha_check() {
        $expiration = time() - 7200; // Limite de dos horas
        $binds = array($this->input->post('captcha'), $this->input->ip_address(), $expiration);
        return $this->cc_model_b->captchaExist($binds);
    }

    private function verifica($usuario, $pass) {
        $result = $this->cc_model_a->verificar_datos($usuario, $pass);
        #existe usuario
        if (is_object($result)) {
            $admin['a_administrador_id'] = $result->a_administrador_id;
            $admin['a_nombre'] = $result->a_nombre;
            $admin['a_mail'] = $result->a_mail;
            $admin['ta_tipo_administrador_id'] = $result->ta_tipo_administrador_id;
            $admin['ta_nombre'] = $result->ta_nombre;
            $this->session->set_userdata('admin', $admin);
            return true;
        }
        # datos no existentes
        return false;
    }

}
